package com.lyc.controller;

import com.lyc.constant.Constant;
import com.lyc.entity.Color;
import com.lyc.entity.Product;
import com.lyc.entity.Size;
import com.lyc.entity.vo.PageBean;
import com.lyc.service.IColorService;
import com.lyc.service.IProductService;
import com.lyc.service.ISizeService;
import com.lyc.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/size")
public class SizeController {
    @Autowired
    private ISizeService sizeService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IColorService colorService;

    @RequestMapping("/sizeList")
    public String sizeList(Integer currentPage, Model model) {
        if (currentPage == null || currentPage <= 0)
            currentPage = 1;
        PageBean<Size> page = sizeService.selectByPage(currentPage, Constant.SIZE_PAGE_SIZE);
        model.addAttribute("page", page);
        return "admin/size_list";
    }

    @RequestMapping("/toAddOrUpdateSizePage")
    public String addOrUpdateColor(Integer sizeId, Model model) {
        if (sizeId != null && sizeId > 0) {
            Size size = sizeService.selectBySizeId(sizeId);
            model.addAttribute("size", size);
        }
        return "admin/size_add";
    }

    @RequestMapping("/addOrUpdateSize")
    public String addOrUpdateColor(Size size) {
        if (size == null || size.getSizeId() == null || size.getSizeId() <= 0) {
            //添加操作
            sizeService.addSize(size);
        } else {
            //修改操作
            sizeService.updateSize(size);
        }
        return "redirect:/size/sizeList";
    }

    @RequestMapping("/deleteSize")
    public String deleteColor(Integer sizeId) {
        if (sizeId != null) {
            //添加操作
            sizeService.deleteSizeBySizeId(sizeId);
        }
        return "redirect:/size/sizeList";
    }

    @RequestMapping("/toOperateSizePage")
    public String toOperateSizePage(Integer pid, Integer type, Model model) {
        Product product = productService.getProductByPid(pid);
        if (product != null) {
            if (type == null)
                type = 1;
            if (type == 1 && product != null && CollectionUtil.notEmpty(product.getSizes())) {
                //尺码操作
                model.addAttribute("sizes", product.getSizes());
                List<Size> sizeList = sizeService.getAll();
                model.addAttribute("sizeList", sizeList);
            } else if (type == 2 && product != null && CollectionUtil.notEmpty(product.getSizes())) {
                //颜色操作
                model.addAttribute("colors", product.getColors());
                List<Color> colorList = colorService.getAll();
                model.addAttribute("colorList", colorList);
            }
        }
        return "/admin/size_operate";
    }

    @RequestMapping("/operateSizeOrColor")
    public String operateSizeOrColor(Integer type, Integer pid, @RequestParam("ids") List<Integer> ids, Model model) {
        String msg = "操作成功";
        if (type == null)
            type = 1;
        if (type == 1 && pid != null) {
            //尺码操作
            sizeService.operateSize(ids, pid);
        } else if (type == 2 && pid != null) {
            //颜色操作
            colorService.operateColor(ids, pid);
        } else {
            msg = "操作失败";
        }
        model.addAttribute("content", msg);
        model.addAttribute("url", "/size/toOperateSizePage?pid=" + pid + "&type=" + type);
        return "/common/errorModal";
    }
}
